package de.hdu.pms.ho.ctrl;

import org.apache.tools.ant.taskdefs.Get;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import de.hdu.pms.ho.dao.PersonalDao;
import de.hdu.pms.ho.model.Kunde;
import de.hdu.pms.ho.model.Personal;

/**Class {@link PersonalController} which initializes the necessary websites for using the program.
 *  Via @RequstMapping will the websites for
 * <ul>
 * <li>editing</li>
 * <li>saving</li>
 * <li>deleting</li> and
 * <li>showing all</li>
 * </ul>
 * be started. 
 * @author Hendrik Neumann
 */
@Controller
public class PersonalController {
	private PersonalDao personalDao = null;
	
	public void setPersonalDao(PersonalDao personalDao) {
		this.personalDao = personalDao;
	}
	
	/**
	 * Allows editing a {@link Personal} in the database with the method {@link Get}.
	 * Starts the website edit-personal.jsp
	 * @param id 	 
	 */
	@RequestMapping(value="/EditPersonal.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
       	ModelAndView mv = new ModelAndView();
   		if (id == null) {
   		mv.addObject(new Personal());
   		} else {
   			mv.addObject(personalDao.findById(id));
   		}
   		mv.setViewName("protected/edit-personal");
   		mv.addObject("title", "Personal");
   		mv.addObject("message", "Personal hinzufügen");
    	return mv;
    }
	
	/**
	 * Saves a {@link Personal} in the database with the method POST.
	 * @param personal 
	 * @param model
	 * @return the website ganzesPersonal.html
	 */
	@RequestMapping(value="/SavePersonal.html", method=RequestMethod.POST)
	public String save(Personal personal, Model model){
		personalDao.save(personal);
        model.addAttribute("statusMessageKey", "personal.form.msg.success");
        return "redirect:GanzesPersonal.html";
    }
	
	/**
	 * Deletes a {@link Personal} from database.
	 * @param personal
	 * @return
	 */
	@RequestMapping(value="/LoeschePersonal.html", method=RequestMethod.GET)
	public ModelAndView delete(Personal personal){
		try {
			personalDao.delete(personal);
			return new ModelAndView("redirect:GanzesPersonal.html");
		} catch (DataIntegrityViolationException e) {
			ModelAndView mv = new ModelAndView("protected/errordelete");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}
	
	/**
	 * Searches for all {@link Personal} and returns them in a Collection with the method {@link Get}.
	 * With {@link Arbeitsplan2pdf} it is possible to make a print of all {@link Personal}
	 * @return
	 */
	@RequestMapping(value="/GanzesPersonal.html", method=RequestMethod.GET)
    public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Personal");
		mv.addObject("message", "Gesamtes Personal");
		mv.addObject("personal1", personalDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("protected/list-personal");
		return mv;
    }


}
